package main

import (
	"database/sql"
	"fmt"
	"math/rand"
	"time"

	_ "github.com/go-sql-driver/mysql"
	//_ "github.com/jmoiron/sqlx"
)

func checkErr(err error) {
	if err != nil {
		fmt.Printf("ERROR:%v\n", err)
		panic(err)
		return
	}

}

func checkErrNopainc(err error) {
	if err != nil {
		return
	}

}

var (
	Conn string = *User + ":" + *Password + "@tcp(" + *Host + ":" + *Port + ")/" + *Database + "?charset=utf8"
)

type account struct {
	id   int
	age  int
	name string
}

func insertDB() {
	db, err := sql.Open("mysql", Conn)
	checkErr(err)
	stmt, err := db.Prepare("INSERT userinfo set username=?,departname=?,created=?")
	checkErr(err)
	res, err := stmt.Exec("张三", "运维", "2020-08-08")
	checkErr(err)
	id, err := res.LastInsertId()
	checkErr(err)
	fmt.Println(id)
	db.Close()
}

func getId() int {
	db, err := sql.Open("mysql", Conn)
	checkErr(err)
	sqlStr := "select max(id) id from user_account "
	var a account
	err = db.QueryRow(sqlStr).Scan(&a.id)
	checkErr(err)
	rand.Seed(time.Now().Unix())
	wid := rand.Intn(a.id)
	var rid int
	err = db.QueryRow("select id from user_account where id=?", wid).Scan(&rid)
	checkErrNopainc(err)
	db.Close()
	return rid
}

func GetSum() int {
	db, err := sql.Open("mysql", Conn)
	checkErr(err)
	var rid int
	err = db.QueryRow("select sum(amount) from (select amount from com_account union all select amount from user_account) b;").Scan(&rid)
	checkErrNopainc(err)
	db.Close()
	return rid
}

func HotAccount() {

	db, err := sql.Open("mysql", Conn)
	checkErr(err)
	xid := getId()
	for xid == 0 {
		xid = getId()
	}
	//fmt.Printf("XID:%d \n", xid)
	rand.Seed(time.Now().Unix())
	randid := rand.Intn(2)
	if randid == 1 {
		Tx, _ := db.Begin()
		stm, _ := Tx.Prepare("update com_account set amount=amount+? where id=1")
		stm.Exec(100)
		stm1, _ := Tx.Prepare("update user_account set amount=amount-? where id=?")
		stm1.Exec(100, xid)
		err = Tx.Commit()
		checkErr(err)
	} else {
		Tx, _ := db.Begin()
		stm, _ := Tx.Prepare("update com_account set amount=amount-? where id=1")
		stm.Exec(100)
		stm1, _ := Tx.Prepare("update user_account set amount=amount+? where id=?")
		stm1.Exec(100, xid)
		err = Tx.Commit()
		checkErr(err)
	}

	db.Close()
}

func Asyncpay() {

	db, err := sql.Open("mysql", Conn)
	checkErr(err)
	xid := getId()
	for xid == 0 {
		xid = getId()
	}
	//fmt.Printf("XID:%d \n", xid)
	rand.Seed(time.Now().Unix())
	randid := rand.Intn(2)
	if randid == 1 {
		Tx, _ := db.Begin()
		stm, _ := Tx.Prepare("insert into user_payment(user_id,amount,status) values(?,?,?);")
		stm.Exec(xid, 100, 1)
		stm1, _ := Tx.Prepare("update user_account set amount=amount+? where id=?")
		stm1.Exec(-100, xid)
		err = Tx.Commit()
		checkErr(err)
	} else {
		Tx, _ := db.Begin()
		stm, _ := Tx.Prepare("insert into user_payment(user_id,amount,status) values(?,?,?);")
		stm.Exec(xid, -100, 1)
		stm1, _ := Tx.Prepare("update user_account set amount=amount+? where id=?")
		stm1.Exec(100, xid)
		err = Tx.Commit()
		checkErr(err)
	}

	db.Close()
}

func Sync() {

	db, err := sql.Open("mysql", Conn)
	checkErr(err)

	Tx, _ := db.Begin()
	stm, _ := Tx.Prepare("update com_account c , (select sum(amount) amount from user_payment where status != ?) p set c.amount=c.amount+ifnull(p.amount,0) ")
	stm.Exec(2)
	stm1, _ := Tx.Prepare("update  user_payment set status=? where status != ?")
	stm1.Exec(2, 2)
	err = Tx.Commit()
	checkErr(err)

	db.Close()
}
